


// 获取 logs 管理页面视图
void Request_View_Logs(XS_ServerObject objServer, XS_HostObject objHost, struct mg_connection* c, struct mg_http_message* hm)
{
	if ( hm->methodCode == HTTP_GET ) {
		
		// 日志页面
		LoadPage(c, 200, HTTP_CT_HTML, "logs.html");
		
	} else {
		
		// 其他请求方法返回 404 页面
		LoadPage(c, 404, HTTP_CT_HTML, "status/404.html");
		
	}
}



// logs 主接口
void Request_Logs(XS_ServerObject objServer, XS_HostObject objHost, struct mg_connection* c, struct mg_http_message* hm)
{
	if ( hm->methodCode == HTTP_GET ) {
		
		// 从数据库读取日志数据
		XDO_Recordset rs = xdoSelect(G_DB, "SELECT * FROM logs ORDER BY id DESC;");
		xvalue data = xvoCreateArray();
		while ( xrsNext(rs) ) {
			xvalue tblRow = xvoCreateTable();
			int64 id = xrtStrToI64(xrsGetValue(rs, 0));
			xvoTableSetInt(tblRow, "id", 2, id);
			xvoTableSetText(tblRow, "user", 4, xrsGetValue(rs, 1), 0, FALSE);
			xvoTableSetText(tblRow, "ip", 2, xrsGetValue(rs, 2), 0, FALSE);
			xvoTableSetText(tblRow, "uri", 3, xrsGetValue(rs, 3), 0, FALSE);
			xvoTableSetText(tblRow, "method", 6, xrsGetValue(rs, 4), 0, FALSE);
			xvoTableSetText(tblRow, "param", 5, xrsGetValue(rs, 5), 0, FALSE);
			xvoTableSetText(tblRow, "body", 4, xrsGetValue(rs, 6), 0, FALSE);
			xtime iTime = xrtStrToI64(xrsGetValue(rs, 7));
			xvoTableSetText(tblRow, "createTime", 10, xrtTimeToStr(iTime, XRT_TIME_FORMAT_DATETIME), 0, TRUE);
			xvoArrayAppendValue(data, tblRow, TRUE);
		}
		xrsFree(rs);
		// 返回接口数据
		xvalue tblRet = xvoCreateTable();
		xvoTableSetBool(tblRet, "result", 6, TRUE);
		xvoTableSetText(tblRet, "message", 7, "日志数据获取成功！", 0, FALSE);
		xvoTableSetValue(tblRet, "data", 4, data, TRUE);
		// 生成 JSON
		size_t iRetSize = 0;
		char* sRet = xrtStringifyJSON(tblRet, FALSE, &iRetSize);
		http_reply(c, 200, "Content-Type: application/json\r\n", sRet, iRetSize);
		xvoUnref(tblRet);
		
	} else if ( hm->methodCode == HTTP_DELETE ) {
		
		// 清空 7 天前的日志
		xtime now = xrtNow();
		xtime sevenDaysAgo = now - (7 * 24 * 60 * 60);
		
		char sql[256];
		sprintf(sql, "DELETE FROM logs WHERE createTime < %lld", sevenDaysAgo);
		xdoExecute(G_DB, sql);
		
		http_reply(c, 200, HTTP_CT_JSON, "{\"result\": true, \"message\": \"7天前的日志已清空！\"}", 0);
		
	} else {
		
		// 其他请求方法返回 404 页面
		LoadPage(c, 404, HTTP_CT_HTML, "status/404.html");
		
	}
}


